﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Context;
using Model.Entity;
using Ones.Dal.Repository.Interfaces;

namespace Ones.Dal.Repository.Impl
{
    public class NewsVersionDao : AbstractDao<NewsVersion>, INewsVersionDao
    {
        public NewsVersionDao()
        {
            _ctx = new OnlineNewsEditorialDataContext();
        }

        public NewsVersionDao(OnlineNewsEditorialDataContext ctx)
        {
            _ctx = ctx;
        }

        /// <summary>
        /// Delete permanently news in news versions table
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public bool delete(int newsId)
        {
            try
            {
                var q = getAll().Where(i => i.newsId == (int)newsId).SingleOrDefault();
                _ctx.NewsVersions.DeleteOnSubmit(q);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">Instance of NewsVersionKey class.</param>
        /// <returns></returns>
        public override NewsVersion
        getById(object id)
        {
            try
            {
                if (!id.GetType().Equals(typeof(NewsVersionKey)))
                {
                    //throw exception that notice about invalid type of param.
                    return null;
                }

                var k = (NewsVersionKey)id;
                return getSingle(i => i.newsId == k.NewsId && i.verId == k.VersionId);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public NewsVersion
        getById(int newsId, int verId)
        {
            return getSingle(i => i.newsId == newsId && i.verId == verId);
        }

        public List<NewsVersion>
        getByNews(int newsId)
        {
            try
            {
                return getMultiple(i => i.newsId == newsId);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<NewsVersion>
        getByVersion(int verId)
        {
            try
            {
                return getMultiple(i => i.verId == verId);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<NewsVersion>
        getByEditor(string verEditor)
        {
            try
            {
                return getMultiple(i => i.verEditor.Equals(verEditor));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<NewsVersion>
        getBySender(string verSender)
        {
            try
            {
                return getMultiple(i => i.verSender.Equals(verSender));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<NewsVersion>
        getByProcessDirection(string processDirection)
        {
            try
            {
                if (processDirection.Equals("Post") || processDirection.Equals("Reject"))
                {
                    var q = from nv in _ctx.NewsVersions
                            where nv.verProcessDirection.Equals(processDirection)
                            select nv;
                    return q.ToList<NewsVersion>();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<NewsVersion>
        getByDate(DateTime verDate)
        {
            try
            {
                return getMultiple(i => i.verDate.Equals(verDate));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<NewsVersion>
        getByDate(int yyyy, int mm, int dd)
        {
            try
            {
                return getMultiple(i => i.verDate.Year == yyyy &&
                                        i.verDate.Month == mm &&
                                        i.verDate.Day == dd);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    #region INewsVersionDao

    public interface INewsVersionDao : IDao<NewsVersion>
    {
        bool delete(int newsId);

        NewsVersion getById(int newsId, int verId);

        List<NewsVersion> getByNews(int newsId);

        List<NewsVersion> getByVersion(int verId);

        List<NewsVersion> getByEditor(String verEditor);

        List<NewsVersion> getBySender(String verSender);

        List<NewsVersion> getByProcessDirection(String processDirection);

        List<NewsVersion> getByDate(DateTime verDate);

        List<NewsVersion> getByDate(int yyyy, int mm, int dd);
    }
    #endregion
}